<extend name="./Application/Admin/View/Layouts/application.html"/>

<block name="css">
    <link rel="stylesheet" href="__PUBLIC__/assets/admin/common.css">
    <style>
        .xChildren {
            display: none;
        }
    </style>
</block>
<block name="content">
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">分类列表</strong> /
                    <small>Category List</small>
                </div>
            </div>

            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{:U('Category/add')}}" class="am-btn am-btn-default"><span
                                    class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="delete_all am-btn am-btn-danger"><span
                                    class="am-icon-trash"></span> 删除
                            </button>
                            <button type="button" id="show_all" class="am-btn am-btn-primary"><span
                                    class="am-icon-arrows"></span> 显示/隐藏分类
                            </button>
                        </div>
                    </div>
                </div>

                <form>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-form-group">
                            <select name="category_id" data-am-selected="{btnWidth: '100%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 360, searchBox: 1}">
                                <volist name="categories" id="category">
                                    <option value="{{$category.id}}">{{$category.name}}</option>
                                    <!--<volist name="category.children" id="child">-->
                                    <!--<option value="{{$child.id}}">-->
                                    <!--&nbsp;&nbsp;&nbsp;&nbsp;|&#45;&#45;{{$child.name}}-->
                                    <!--</option>-->
                                    <!--</volist>-->
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" name="name" class="am-form-field">
                            <span class="am-input-group-btn">
                                <button class="am-btn am-btn-default" type="submit">搜索</button>
                            </span>
                        </div>
                    </div>
                </form>

            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>ID</th>
                                <th>分类名</th>
                                <th>分类商品</th>
                                <th>是否显示</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            <volist name="categories" id="category">
                                <tr data-id="{{$category.id}}" id="row_{{$category.id}}" class="xParent">
                                    <td>
                                        <input type="checkbox" name="ids[]" value="{{$category.id}}"
                                               class="checked_id"/>
                                    </td>
                                    <td>{{$category.id}}</td>
                                    <td>{{$category.name}}</td>
                                    <td></td>
                                    <td data-attr="{{$category.is_show}}">
                                        <eq name="category.is_show" value="1">
                                            <span class="am-icon-check is_show"></span>
                                            <else/>
                                            <span class="am-icon-close is_show"></span>
                                        </eq>
                                    </td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$category.id}}">
                                        <input type="text" name="sort_order[]" value="{{$category.sort_order}}"
                                               class="sort_order am-input-sm">
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{:U('edit',['id'=>$category[id]])}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                        class="am-icon-pencil-square-o"></span> 编辑
                                                </a>

                                                <a href="javascript:void (0);"
                                                   class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash"></span> 删除
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>

                                <volist name="category.children" id="child">
                                    <tr data-id="{{$child.id}}" id="row_{{$child.id}}" class="xChildren">
                                        <td>
                                            <input type="checkbox" name="ids[]" value="{{$child.id}}"
                                                   class="checked_id"/>
                                        </td>
                                        <td>{{$child.id}}</td>
                                        <td>
                                            &nbsp;&nbsp;&nbsp;&nbsp;|--{{$child.name}}
                                        </td>
                                        <td>
                                            <a href="{{:U('Product/index',['category_id'=>$child[id]])}}"
                                               class="am-btn am-btn-primary am-btn-xs">查看商品</a>
                                        </td>
                                        <td data-attr="{{$child.is_show}}">
                                            <eq name="child.is_show" value="1">
                                                <span class="am-icon-check is_show"></span>
                                                <else/>
                                                <span class="am-icon-close is_show"></span>
                                            </eq>
                                        </td>
                                        <td>
                                            <input type="hidden" name="id[]" value="{{$child.id}}">
                                            <input type="text" name="sort_order[]" value="{{$child.sort_order}}"
                                                   class="sort_order am-input-sm">
                                        </td>
                                        <td>
                                            <div class="am-btn-toolbar">
                                                <div class="am-btn-group am-btn-group-xs">
                                                    <a href="{{:U('edit',['id'=>$child[id]])}}"
                                                       class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                    </a>

                                                    <a href="javascript:void (0);"
                                                       class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                        <span class="am-icon-trash"></span> 删除
                                                    </a>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                </volist>
                            </volist>

                            </tbody>
                        </table>
                    </form>
                    <div class="am-cf">
                        <div class="am-fr">
                            <ul class="am-pagination">

                            </ul>
                        </div>
                    </div>
                    <hr/>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

        <!--删除模态框-->
        <div class="am-modal am-modal-confirm" tabindex="-1" id="deleteCategory">
            <div class="am-modal-dialog">
                <input type="hidden" id="delete_id">

                <div class="am-modal-hd">删除分类</div>
                <div class="am-modal-bd">
                    你，确定要删除这个分类吗？
                </div>
                <div class="am-modal-footer">
                    <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                    <span class="delete_confirm am-modal-btn" data-am-modal-confirm>确定</span>
                </div>
            </div>
        </div>

    </div>
</block>

<block name="js">
    <script>
        $(function () {
            //ajax删除
            $('#doc-modal-list').find('.am-icon-close').add('.delete').on('click', function () {
                $('#deleteCategory').modal();
                var id = $(this).parents('tr').data('id');
                $("#delete_id").val(id);
            });

            $(".delete_confirm").click(function () {
                var id = $("#delete_id").val();
                var _this = $("tr[data-id='" + id + "']");

                $.post("{{:U('ajax_delete')}}", {id: id}, function (info) {
                    if (info.status == 1) {
                        _this.fadeOut(400);
                        $('#deleteCategory').modal().hide();
                        alert(info.msg);
                    } else {
                        $('#deleteCategory').modal().hide();
                        alert(info.msg);
                    }
                })
                return false;
            });
        })

        //全选和不全选
        $(".check_all").click(function () {
            var checked = $(this).prop('checked');
            $('.checked_id').prop('checked', checked);
        })

        //下面全选中上面也全选
        $(".checked_id").click(function () {
            var allLength = $(".checked_id").length;
            var length = $('.checked_id:checked').length;
            if (allLength == length) {
                $('.check_all').prop('checked', true);
            } else {
                $(".check_all").prop('checked', false);
            }
        })

        //ajax多选删除
        $(".delete_all").click(function () {
            var length = $('.checked_id:checked').length;
            if (length == 0) {
                alert('请至少选中一条');
                return false;
            }

            $.post("{{:U('ajax_delete_all')}}", $(".checked_id:checked").serialize(), function (info) {
                if (info.status == 1) {
                    $(".checked_id:checked").each(function () {
                        $(this).parents('tr').fadeOut(400);
                    })
                    alert(info.msg);
                }
            })
            return false;
        })

        //ajax排序
        $(".sort_order").change(function () {
            var info = {
                id: $(this).parents('tr').data('id'),
                sort_order: $(this).val()
            }

            $.post("{{:U('ajax_sort_order')}}", info, function () {
                window.location.reload();
            })

            return false;
        })

        //ajax改变显示状态
        $(".is_show").click(function () {
            var info = {
                id: $(this).parents('tr').data('id'),
                is_show: $(this).parents('td').data('attr')
            };

            var _this = $(this);
            $.post("{{:U('ajax_is_show')}}", info, function () {
                _this.toggleClass('am-icon-check am-icon-close')
            })
            return false;
        })

        //ajax显示隐藏
        $("tr.xParent").dblclick(function () {
            $(this).toggleClass();
            $(".child_" + this.id).toggle();
        })

        $("#show_all").click(function () {
            $("tr.xParent").toggleClass();
            $("tr.xChildren").toggle('am-active');
        })

    </script>
</block>